package cjgl.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cjgl.entity.Student;
import cjgl.entity.Teacher;
import cjgl.service.StudentService;
import cjgl.service.TeacherService;

/**
 * Servlet implementation class pwd
 */
@WebServlet("/pwd")
public class Pwd extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private StudentService studentService = new StudentService();
	private TeacherService teacherService = new TeacherService();
		
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method =request.getParameter("method");
		if("xs".equals(method)) {
			xs(request,response);
		}else if("zx".equals(method)) {
			zx(request,response);
		}else if("ls".equals(method)) {
			ls(request,response);
		}else if ("xspwd".equals(method)) {
			xspwd(request,response);
		}else if("lspwd".equals(method)) {
			lspwd(request,response);
		}
	}

	private void lspwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Teacher tea =(Teacher)session.getAttribute("user");
		String oldpassword = request.getParameter("oldpassword");
		String password = request.getParameter("password");
		String newpassword = request.getParameter("newpassword");
		if(tea.getPassword().equals(oldpassword)) {
			if(!"".equals(password)) {
					if(password.equals(newpassword)) {
						tea.setPassword(newpassword);
						teacherService.save(tea);
						session.removeAttribute("user");
						response.sendRedirect("login.jsp");
					}else{
						request.setAttribute("cuowu","两次密码输入不一样");
						request.getRequestDispatcher("jsp/pwd/lspwd.jsp").forward(request, response);
					}
			}else {
				request.setAttribute("cuowu","请输入新密码");
				request.getRequestDispatcher("jsp/pwd/lspwd.jsp").forward(request, response);
			}
		}else{
			request.setAttribute("cuowu","原密码输入错误");
			request.getRequestDispatcher("jsp/pwd/lspwd.jsp").forward(request, response);
		}
	}

	private void xspwd(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		HttpSession session = request.getSession();
		Student stu =(Student)session.getAttribute("user");
		String oldpassword = request.getParameter("oldpassword");
		String password = request.getParameter("password");
		String newpassword = request.getParameter("newpassword");
		if(stu.getPassword().equals(oldpassword)) {
			if(!"".equals(password)) {
				if(password.equals(newpassword)){
					stu.setPassword(newpassword);
					studentService.save(stu);
					session.removeAttribute("user");
					response.sendRedirect("login.jsp");
				}else{
				request.setAttribute("cuowu","两次密码输入不一样");
				request.getRequestDispatcher("jsp/pwd/xspwd.jsp").forward(request, response);
				}
			}else{
				request.setAttribute("cuowu","请输入新密码");
				request.getRequestDispatcher("jsp/pwd/lspwd.jsp").forward(request, response);
			}
		}else{
			request.setAttribute("cuowu","原密码输入错误");
			request.getRequestDispatcher("jsp/pwd/xspwd.jsp").forward(request, response);
		}
	
	}



	private void ls(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Teacher stu = (Teacher)session.getAttribute("user");
		request.setAttribute("model",stu);
		request.getRequestDispatcher("jsp/pwd/lspwd.jsp").forward(request, response);
	}

	private void zx(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		response.sendRedirect("login.jsp");
	}


	private void xs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Student stu = (Student)session.getAttribute("user");
		request.setAttribute("model",stu);
		request.getRequestDispatcher("jsp/pwd/xspwd.jsp").forward(request, response);
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
